//
//  AnimationLoader.m
//  pipi
//
//  Created by zhao ys on 5/17/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AnimationLoader.h"

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

static AnimationLoader * _sharedAnimationLoader = nil;

@implementation AnimationLoader

+(AnimationLoader*) sharedLoader {
	if (_sharedAnimationLoader == nil) {
		_sharedAnimationLoader = [[AnimationLoader alloc] init];
	}
	return _sharedAnimationLoader;
}

static int l_LoadTexture(lua_State *L) 
{
	if (!lua_isstring(L, -1)) {
		return 0;
	}
	
	const char * plist = lua_tostring(L, -1);
	
	[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"%s", plist]];
	return 0;
}

int LuaTable_ForEach(lua_State * L, int (*cb)(lua_State*, void *), void * p)
{
	if (!lua_istable(L, -1)) {
		return 0;
	}
	
	int nTop = lua_gettop(L);
	lua_pushnil(L);
	while(lua_next(L, nTop)) {
		int top = lua_gettop(L);
		if (cb(L, p) != 0) {
			// pop key value and return
			//assert(top == lua_gettop(L));
			lua_pop(L, 2);
			return -1;
		}
		assert(top == lua_gettop(L));
		lua_pop(L, 1);
	}
	return 0;
}

static int LoadFrame(lua_State * L, void * p)
{
	NSMutableArray * frames = (NSMutableArray*)p;
	
	int delay = 0;
	lua_pushnumber(L, 2);
	lua_gettable(L, -2);
	delay = lua_tointeger(L, -1);
	lua_pop(L, 1);
	
	lua_pushnumber(L, 1);
	lua_gettable(L, -2);
	const char * file = lua_tostring(L, -1);
	CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%s", file]];
	/*
	if (frame == 0) {
		CCTexture2D * texture = [[CCTextureCache sharedTextureCache] addImage:GetFullPath(file)];
		CGRect rect;
		rect.origin = CGPointZero;
		rect.size = texture.contentSize;
		frame = [CCSpriteFrame frameWithTexture:texture rect:rect];
	}
	*/
	
	int i;
	for(i = 0; i < delay; i++) {
		[frames addObject:frame];
	}
	lua_pop(L, 1);
	return 0;
}


static CCAnimation * CreateAnimationFromLuaTable(lua_State *L, CCSpriteFrame ** firstFrame)
{
	if (!lua_istable(L,-1)) {
		return 0;
	}
	
	NSMutableArray * frames = [NSMutableArray arrayWithObjects:nil];
	
	LuaTable_ForEach(L, LoadFrame, frames);
	if (frames.count == 0) {
		return 0;
	}
	
	if (firstFrame) {
		*firstFrame = [frames objectAtIndex:0];
	}
	CCAnimation *animation = [CCAnimation animationWithFrames:frames delay:(float)1.0/60];
	return animation;
}


static int LoadAnimation(lua_State * L, void * p)
{
	if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
		return 0;
	}

	const char * AnimationName = lua_tostring(L, -2);
	printf("Load Animation: %s\n", AnimationName);
	
	
	CCAnimation * animation = CreateAnimationFromLuaTable(L, 0);
	if (animation == 0 ) {
		return 0;
	}
	
	[[CCAnimationCache sharedAnimationCache] addAnimation:animation name:[NSString stringWithFormat:@"%s", AnimationName]];
	return 0;
}

-(BOOL) LoadAnimationFromFile:(NSString *)script {
	NSString *file = [script lastPathComponent];
	NSString *directory = [script stringByDeletingLastPathComponent];
	
	NSString * fullpath = [[NSBundle mainBundle] pathForResource:file
											   ofType:nil
										  inDirectory:directory];
	
	NSLog(@"%@", fullpath);
	
	lua_State * L = luaL_newstate();
	luaL_openlibs(L);
	
	lua_pushcfunction(L, l_LoadTexture);
	lua_setglobal(L, "LoadTexture");
	
	if (luaL_dofile(L, [fullpath UTF8String]) != 0) {
		lua_close(L);
		return NO;
	}
	
	lua_getglobal(L, "Animation");
	if (lua_istable(L, -1)) {
		LuaTable_ForEach(L, LoadAnimation, 0);
	} 
	lua_pop(L, 1); 
	
	return YES;
}

-(CCAnimation*) animationByName:(NSString*)name {
	return [[CCAnimationCache sharedAnimationCache] animationByName:name];
}
@end
